
<template>
    <div class="app-container">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">            
            <el-form-item label="实体" prop="entityId" >                
                <el-input v-model="entityTitle" placeholder="请选择实体" :readonly="true" >
                    <el-button slot="append" icon="el-icon-search" @click="entityIdOpen=true"></el-button>
                </el-input>
                <el-dialog title="从已有实体中选择或新增实体" :visible.sync="entityIdOpen" width="800px" append-to-body :close-on-click-modal="false">
                    <entityIdRefer ref="entityIdRefer"                        
                    ></entityIdRefer>
                    <div slot="footer" class="dialog-footer">
                        <el-button type="primary" @click="entityIdConfirm">确 定</el-button>
                        <el-button @click="entityIdCancel">取 消</el-button>
                    </div>
                </el-dialog>                
            </el-form-item>            
            <el-form-item label="说明" prop="desc" >
                <el-input v-model="form.desc" type="textarea" placeholder="请输入说明"  />
            </el-form-item>            
        </el-form>        
    </div>
</template>
<script>
import {
    getDemoRecord,
    createDemoRecord,
    updateDemoRecord,    
    listEntityId,    
} from "@/api/demo/demoRecord";
import { getUserProfile } from "@/api/system/user";
import {genCode} from '@/api/code.js';
import dayjs from 'dayjs'
export default {
    name: "DemoRecordEditor",
    components:{        
        entityIdRefer:()=>import("@/views/demo/demoEntity/list/refer.vue"),        
    },
    props: [        
    ],
    data() {
        return {            
            // entityIdOptions关联表数据
            entityIdOptions: [],            
            entityIdOpen:false,
            entityTitle: "",            
            // 表单参数
            form: {},
            params: {},
            // 表单校验
            rules: {                
            },
            user:{}
        };
    },
    methods: {
        // 表单重置
        async reset() {            
            // 重新加载选项            
            // 重置表单值
            this.form = {                
                id: undefined,                
                entityId: undefined,                
                desc: undefined,                
            };
            // 重置相关参数            
            this.resetForm("form");
        },        
        show(){
            this.reset();
        },        
        // 提前提交
        precreate(){
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (!this.form.id) {
                        createDemoRecord(this.form).then(response => {
                            if (response.code === 0) {
                                this.form.id = response.data.id;
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    }
                }
            });
        },
        // 正常提交
        submitForm: function(done) {
            this.$refs["form"].validate(valid => {
                if (valid) {                    
                    if (this.form.id != undefined) {
                        updateDemoRecord(this.form).then(response => {
                            if (response.code === 0) {
                                this.msgSuccess("修改成功");
                                done()
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    } else {
                        createDemoRecord(this.form).then(response => {
                            if (response.code === 0) {
                                this.msgSuccess("新增成功");
                                done()
                            } else {
                                this.msgError(response.msg);
                            }
                        });
                    }
                }
            });
        },
        genCode(){            
        },
        getUser() {
            return getUserProfile().then(response => {
                this.user = response.data;
            });
        },        
        //关联m_demo_entity表选项
        getEntityIdItems() {
            return this.getItems(listEntityId, {pageSize:10000}).then(res => {
                this.entityIdOptions = this.setItems(res, 'id', 'title')
            })
        },        
        entityIdConfirm(){
            let row = this.$refs.entityIdRefer.referredRow;
            this.form.entityId = row.id;
            this.entityTitle = row.title;
            this.entityIdOpen = false;
        },
        entityIdCancel() {
            this.entityIdOpen = false;
        },        
    }
};
</script>
<style>    
</style>